import { Component ,OnInit} from '@angular/core';
import { appService } from '../../../../my-service/app.service';
import { fadeIn } from '../../../../animate';
import { UtilService } from '../../../../my-service/util.service'
import { Router, ActivatedRoute } from '@angular/router';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
@Component({
  selector: 'canvaslist-root',
  templateUrl: './canvaslist.component.html',
  styleUrls: ['./canvaslist.component.css'],
  animations: [fadeIn]
})
export class canvaslistComponent implements OnInit{
  constructor(
    private appservice: appService,
    private router: Router,
    private utilService: UtilService,
    private activatedRoute: ActivatedRoute) { }
  breadcrumb = {
    name: '所有训练',
    path: '',
    children: {
      name: '加载中。。。'
    }
  }
  trainmsg = {
    CreateIP: '',
    CreateTime: '',
    CreaterName: '',
    CreaterOrgName: '',
    Description: '',
    FK_CanvasGuid: '',
    FK_ClassGuid: '',
    FK_CreatorGuid: '',
    ID: '',
    PK_TrainningGuid: '',
    Status: '',
    Title: '加载中。。。',
    Code:null,
    CodeOverdue:false
  }
  FK_TrainningGuid = this.activatedRoute.snapshot.paramMap.get('FK_TrainningGuid');;
  canvaslist = []
  nzTotal;
  nzPageIndex = 1;
  smcanvaslist = [];
  nzPageSize = 6;
  load = false;
  addwindow = false;
  form = {
    Name: '',
    Password: '0000'
  }
  ngOnInit() {
    this.shuiji()
    this.appservice.trainmsg(this.FK_TrainningGuid).then(re => {
      this.trainmsg = re
      this.breadcrumb.children.name = re.Title

      this.utilService.reverse(this.trainmsg.PK_TrainningGuid).then(re2=>{

        if(re2.status==2){
          this.trainmsg.CodeOverdue=true
        }
      })
    })
    this.viewload()
  }
  redraw(){
    if(this.trainmsg.CodeOverdue){
      let apiConfig={
        projectName:'bc',
        guid:this.trainmsg.PK_TrainningGuid,
        endTime: this.utilService.timestamp(new Date())+2592000,
        FK_ClassGuid:this.trainmsg.FK_ClassGuid
      }
      this.utilService.createCode(apiConfig).then(rec=>{

        this.appservice.bindCode(apiConfig.guid,rec.code).then((re6)=>{
          this.trainmsg.Code=rec.code
          this.trainmsg.CodeOverdue=false
        })
      })
    }


  }
  viewload() {
    this.smcanvaslist = []
    this.appservice.getcanvasgroup(this.FK_TrainningGuid, 0, 10000).then(re2 => {
      this.canvaslist = this.ColorAndNum(re2);
      this.nzTotal = re2.length;
      let len: number;
      if (this.nzTotal > this.nzPageSize) len = this.nzPageSize
      else if (this.nzTotal <= this.nzPageSize) len = this.nzTotal
      for (let i = 0; i < len; i++) {
        this.smcanvaslist.push(re2[i])
      }
    }).then(() => {
      this.load = true;
    })
  }
  initialize() {
    this.form = {
      Name: ``,
      Password: this.getCode(4)
    }
  }
  //获取canvasgroup的color和有多少个标签
  ColorAndNum(canvasgroup) {
    canvasgroup.forEach(element => {
      this.appservice.getNoteNum(element.PK_CanvasGroupGuid).then(re2 => {
        element.Num = re2;
        if (re2 >= 0 && re2 < 40) { element.Backcolor = "#FC9399" }
        else if (re2 >= 40 && re2 < 60) { element.Backcolor = "#E56599" }
        else { element.Backcolor = "#CD3699" }
      })
    })
    return canvasgroup
  }
  shuiji() {
    this.form.Password = this.getCode(4);
  }
  getCode(num) {
    let id = "";
    for (let i = 1; i <= num; i++) {
      let n = Math.floor(Math.random() * 10.0).toString(10);
      id += n;

    }
    return id;
  }
  pagin() {
    this.smcanvaslist = []
    let n = (this.nzPageIndex - 1) * this.nzPageSize;
    for (let i = 0; i < this.nzPageSize; i++) {
      if ((n + i) < this.canvaslist.length) {
        this.smcanvaslist.push(this.canvaslist[i + n])
      }
    }
  }
  checknum(num, item) {
    if (item == 'Password') {
      if (JSON.stringify(this.form[item]).length > num) {
        this.form[item] = parseInt(JSON.stringify(this.form[item]).slice(0, num))
      }
    } else {
      if (this.form[item].length > num) {
        this.form[item] = this.form[item].slice(0, num)
      }
    }
  }
  getDate() {
    let date = new Date(); //取当前时间
    let month = date.getMonth() + 1;
    let months, strDates;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      months = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDates = "0" + strDate;
    } else {
      strDates = strDate
    }
    let currentdate = months + strDates;
    return currentdate;
  }
  add() {
    if (this.form.Name == '') {
      this.utilService.remind('error', '名字不能为空!')
      return
    }
    if (this.form.Password.length != 4) {
      this.utilService.remind('error', '请填写四位密码!')
      return
    }
    this.appservice.postNewCanvasGroup(this.form, this.FK_TrainningGuid).then((re) => {
      if (re.status == "success") {
        this.viewload();
        this.initialize();
        this.addwindow = false;
      }
    })
  }
  goto(path) {
    this.router.navigate([`${path}`])
  }
}
